// index.ts
import {request} from "../request/index.js";
import {config} from "../request/config.js";
Page({
  data: {
    defaultImageUrl:'../../imgs/default.png',
   goodsInfoCarouselList:[], //轮播图列表
   goodsInfoRecommendList:[] //是否为推荐商品
   
  },
  onLoad() {
    this.getGoodsInfoCarouselList();
    this.goodsInfoRecommendList();
  },
 //获取轮播图
 getGoodsInfoCarouselList(){
    request({url:'/goodsInfo/page/all?pageNum=1&pageSize=3'}).then(res=>{
        if(res.code === '0'){
          let goodsInfoCarouselList = res.data.list;
          if(!goodsInfoCarouselList || goodsInfoCarouselList.lenght ===0){
            goodsInfoCarouselList.push({"name":"名称1", "url":this.data.defaultImageUrl});
            goodsInfoCarouselList.push({"name":"名称1", "url":this.data.defaultImageUrl});
            goodsInfoCarouselList.push({"name":"名称1", "url":this.data.defaultImageUrl});
          }else{
              if(goodsInfoCarouselList.lenght >4){
                goodsInfoCarouselList = goodsInfoCarouselList.slice(0, 4)
              }
              goodsInfoCarouselList.forEach(res => {
                  if(!res.fileIds || res.fileIds === '[]'){
                    res.url = this.data.defaultImageUrl;
                  }else{
                      let fileArr = JSON.parse(res.fileIds);
                      res.url = config.baseFileUrl + fileArr[0];
                  }
              });
          }
          this.setData({
            goodsInfoCarouselList
          })
        }else{
           wx.showToast({
               title:res.msg,
               icon:'none'
           })
        }
    })
 },

 //获取推荐商品
 goodsInfoRecommendList(){
    request({url:'/goodsInfo/findRecommendGoods'}).then(res=>{
        if(res.code === '0'){
          let goodsInfoRecommendList = res.data.list;
          if(!goodsInfoRecommendList || goodsInfoRecommendList.lenght ===0){
            goodsInfoRecommendList.push({"name":"名称1", "url":this.data.defaultImageUrl});
            goodsInfoRecommendList.push({"name":"名称1", "url":this.data.defaultImageUrl});
            goodsInfoRecommendList.push({"name":"名称1", "url":this.data.defaultImageUrl});
          }else{
              if(goodsInfoRecommendList.lenght >4){
                goodsInfoRecommendList = goodsInfoRecommendList.slice(0, 4)
              }
              goodsInfoRecommendList.forEach(res => {
                  if(!res.fileIds || res.fileIds === '[]'){
                    res.url = this.data.defaultImageUrl;
                  }else{
                      let fileArr = JSON.parse(res.fileIds);
                      res.url = config.baseFileUrl + fileArr[0];
                  }
              });
          }
          this.setData({
            goodsInfoRecommendList
          })
        }else{
           wx.showToast({
               title:res.msg,
               icon:'none'
           })
        }
    })
 },
 
})
